Skip to content

Conversation

@harshampatel-sysdig
Copy link
Collaborator

@harshampatel-sysdig harshampatel-sysdig commented Oct 30, 2025

Key changes:

  • Added resource and data source for sysdig_secure_okta_ml_policy
  • Implemented CRUD operations with proper error handling
  • Added tests and documentation

Similar open PR: #675 (I'll close this once new one merged)

ombellare
ombellare previously approved these changes Oct 31, 2025
Copy link
Contributor

@ombellare ombellare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Changed threshold value descriptions from 'High/Medium/Low' to 'Default/High/Higher' for ML policies
ombellare
ombellare previously approved these changes Oct 31, 2025
Copy link
Member

@tembleking tembleking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @harshampatel-sysdig, thank you for this implementation, it's really nice.
I left you some comments to address before merging it, with some small improvements we can benefit from.

Comment on lines 325 to 326
"enabled": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Enabled,
"threshold": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Threshold,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if at some point rule.Details is not a *v2.OktaMLRuleDetails or AnomalousConsoleLogin is nil? We should check this for robustness.

For example

if d, ok := rule.Details.(*v2.OktaMLRuleDetails); ok && d.AnomalousConsoleLogin != nil {
    enabled := d.AnomalousConsoleLogin.Enabled
    threshold := int(d.AnomalousConsoleLogin.Threshold)
    // ...
}

Comment on lines 325 to 326
"enabled": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Enabled,
"threshold": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Threshold,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are passing from Threshold (float64) to int, because in MLRuleThresholdAndSeveritySchema we are defining it as TypeInt.

"threshold": {
	Type:     schema.TypeInt,
	Required: true,
},

Either we define the Threshold as int in the model, or we transform it:

Suggested change
"enabled": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Enabled,
"threshold": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Threshold,
"enabled": rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Enabled,
"threshold": int(rule.Details.(*v2.OktaMLRuleDetails).AnomalousConsoleLogin.Threshold),

I prefer the first solution, to define the Threshold as int because in the documentation you also specify:

`threshold` - Confidence level threshold for triggering alerts. Valid values are: 1 (Default), 2 (High), 3 (Higher).


# Resource: sysdig_secure_okta_ml_policy

Retrieves the information of an existing Sysdig Secure Okta ML Policy.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resources not only retrieve info, they also manage.

Suggested change
Retrieves the information of an existing Sysdig Secure Okta ML Policy.
Manages a Sysdig Secure Okta ML Policy.

Comment on lines 17 to 32
```terraform
resource "sysdig_secure_okta_ml_policy" "policy" {
name = "Test Okta ML Policy 1"
description = "Test Okta ML Policy Description"
enabled = true
severity = 4
rule {
description = "Test Okta ML Rule Description"
anomalous_console_login {
enabled = true
threshold = 1
}
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing a } in the example:

Suggested change
```terraform
resource "sysdig_secure_okta_ml_policy" "policy" {
name = "Test Okta ML Policy 1"
description = "Test Okta ML Policy Description"
enabled = true
severity = 4
rule {
description = "Test Okta ML Rule Description"
anomalous_console_login {
enabled = true
threshold = 1
}
}
```
```terraform
resource "sysdig_secure_okta_ml_policy" "policy" {
name = "Test Okta ML Policy 1"
description = "Test Okta ML Policy Description"
enabled = true
severity = 4
rule {
description = "Test Okta ML Rule Description"
anomalous_console_login {
enabled = true
threshold = 1
}
}
}

* `description` - (Required) Rule description.
* `anomalous_console_login` - (Required) This attribute allows you to activate anomaly detection for logins and adjust its settings.
* `enabled` - (Optional) Whether anomaly detection is enabled. Defaults to `true`.
* `threshold` - (Required) Trigger at or above confidence level. Valid values are: 1 (Default), 2 (High), 3 (Higher).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since threshold can only be 3 values, we should validate it in the Terraform schema with something like:

validation.IntInSlice([]int{1,2,3})

if _, ok := d.GetOk("rule"); ok {

anomalousLogin := &v2.MLRuleThresholdAndSeverity{}
if _, ok := d.GetOk("rule.0.anomalous_console_login"); ok { // TODO: Do not hardcode the indexes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one rule can be created for this policy type? If so, then rule.0.* is fine here 👍 We can change it if we start supporting multiple rules.
Nevertheless, please add a MaxItems: 1 to the block:

			"rule": {
				Type:     schema.TypeList,
				Required: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"id":   ReadOnlyIntSchema(),
						"name": ReadOnlyStringSchema(),
						// Do not allow switching off individual rules
						// "enabled":     EnabledSchema(),
						"description":             DescriptionSchema(),
						"tags":                    TagsSchema(),
						"version":                 VersionSchema(),
						"anomalous_console_login": MLRuleThresholdAndSeveritySchema(),
					},
				},
			},

return diag.FromErr(err)
}

policy, err := oktaMLPolicyFromResourceData(d)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to construct a whole policy from the resource data just to grab the ID? Maybe we can just read d.Id().

})
}

func oktaOktaMLPolicyDataSource(name string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird name here 😄 did you meant oktaMLPolicyDataSource?

Suggested change
func oktaOktaMLPolicyDataSource(name string) string {
func oktaMLPolicyDataSource(name string) string {

Comment on lines 32 to 34
{
Config: oktaOktaMLPolicyDataSource(rText),
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not checking that the parameters are correctly received and assigned to the data source.

Suggested change
{
Config: oktaOktaMLPolicyDataSource(rText),
},
{
Config: oktaOktaMLPolicyDataSource(rText),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.sysdig_secure_okta_ml_policy.policy_2", "name", fmt.Sprintf("Test Okta ML Policy %s", rText)),
resource.TestCheckResourceAttr("data.sysdig_secure_okta_ml_policy.policy_2", "description", fmt.Sprintf("Test Okta ML Policy Description %s", rText)),
resource.TestCheckResourceAttr("data.sysdig_secure_okta_ml_policy.policy_2", "enabled", "true"),
resource.TestCheckResourceAttr("data.sysdig_secure_okta_ml_policy.policy_2", "severity", "4"),
),
},

@harshampatel-sysdig
Copy link
Collaborator Author

Thank you Fede for reviewing and providing valuable feedback. I have pushed changes to cover feedback points. Seems like my commits aren't taking secrets and failing same common test cases. Could you please push empty commit from your end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants